package com.example.demo.controller;

import java.io.BufferedReader;
import java.io.FileReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.dao.SysuserMapper;
import com.example.demo.domain.Project_details;
import com.example.demo.domain.T_box_dataDictionary;
import com.example.demo.domain.T_box_dataDictionaryOne;
import com.example.demo.services.T_box_dataDictionaryService;
import com.example.demo.util.MergeFileUtils;
import com.example.demo.util.Util;

import net.sf.json.JSONObject;

@RestController
@RequestMapping("/boxDataDictionary")
public class T_box_dataDictionaryController {
	private static Logger logger = Logger.getLogger(T_box_dataDictionaryController.class);

	@Autowired
	private T_box_dataDictionaryService t_box_dataDictionaryService;

	/**
	 * 处理t-box数据字典
	 * 
	 * @author dsh
	 * @param request
	 * @param response
	 * @return List<Attachment>
	 */
	@RequestMapping(value = "/selectALL")
	public String selectALL(HttpServletRequest request, HttpServletResponse response) throws Exception {
		JSONObject obj = new JSONObject();

		try {

			List<T_box_dataDictionary> box = t_box_dataDictionaryService.selectALL();

			obj.put("data", box);
			obj.put(Util.RESULT, Util.RESULT_RC_SUCCESS);
			obj.put(Util.MESSAGE, "查询指定常规变量数据及时间戳成功！");
		} catch (Exception e) {
			logger.error("=============[T_box_dataDictionaryController[selectALL[查询所有常规变量配置信息]]]==========", e);
		}
		return obj.toString();
	}
	
	/**
	 * 处理t-box数据字典1
	 * 
	 * @author dsh
	 * @param request
	 * @param response
	 * @return List<Attachment>
	 */
	@RequestMapping(value = "/selectALLOne")
	public String selectALL1(HttpServletRequest request, HttpServletResponse response) throws Exception {
		JSONObject obj = new JSONObject();

		try {

			List<T_box_dataDictionaryOne> box = t_box_dataDictionaryService.selectALLOne();

			obj.put("data", box);
			obj.put(Util.RESULT, Util.RESULT_RC_SUCCESS);
			obj.put(Util.MESSAGE, "查询指定常规变量数据及时间戳成功！");
		} catch (Exception e) {
			logger.error("=============[T_box_dataDictionaryController[selectALLOne[查询所有常规变量配置信息]]]==========", e);
		}
		return obj.toString();
	}

	/*	*//**
			 * 处理t-box数据字典
			 * 
			 * @author dsh
			 * @param request
			 * @param response
			 * @return List<Attachment>
			 *//*
				 * @RequestMapping(value = "/dataDictionary") public String
				 * dataDictionary(HttpServletRequest request, HttpServletResponse response)
				 * throws Exception { Map<String, Object> map = new HashMap<String, Object>();
				 * JSONObject obj = new JSONObject();
				 * 
				 * try { long time_stamp = 0;//记录最新的时间戳 ArrayList dataList = new ArrayList();
				 * ArrayList time_stampList = new ArrayList();
				 * 
				 * List<T_box_dataDictionary> box =
				 * t_box_dataDictionaryService.selectByname(request.getParameter("nameText"));
				 * String fileName = request.getParameter("filePath"); FileReader fileReader =
				 * new FileReader(fileName); BufferedReader bufferedReader = new
				 * BufferedReader(fileReader); String line =bufferedReader.readLine();
				 * 
				 * while (line!=null){ if(line.substring(0, 2) == "19" || line.substring(0,
				 * 2).equals("19")) { time_stamp = dateToStamp(line.substring(0, 17)); }
				 * if(line.substring(0, 2) == box.getFrame_number() || line.substring(0,
				 * 2).equals(box.getFrame_number() )) { time_stamp++;
				 * dataList.add(strTo16(line.substring(box.getStart_byte(), box.getEnd_byte()),
				 * box.getCoefficient())); time_stampList.add(time_stamp); } line =
				 * bufferedReader.readLine(); }
				 * 
				 * bufferedReader.close(); fileReader.close();
				 * 
				 * map.put("dataList", dataList); map.put("time_stampList", time_stampList);
				 * obj.put("data", map); obj.put(Util.RESULT, Util.RESULT_RC_SUCCESS);
				 * obj.put(Util.MESSAGE, "查询指定常规变量数据及时间戳成功！"); } catch (Exception e) {
				 * logger.error(
				 * "=============[T_box_dataDictionaryController[dataDictionary[查询指定常规变量数据及时间戳]]]==========",
				 * e); } return obj.toString(); }
				 */

	public static void main(String[] args) {
		try {
			String fileName = "C:\\Users\\DELL\\Desktop\\新建文件夹 (2)\\190514\\0x6f3.txt";
			FileReader fileReader = new FileReader(fileName);
			BufferedReader bufferedReader = new BufferedReader(fileReader);
			String line = bufferedReader.readLine();

			while (line != null) {
				if (line.substring(0, 2) == "19" || line.substring(0, 2).equals("19")) {
					System.out.println(line.substring(0, 17));
				}
				line = bufferedReader.readLine();
			}
			List l = new ArrayList();

			bufferedReader.close();
			fileReader.close();

		} catch (Exception e) {
			logger.error("=============[ActivitiController[addAttachments[标定数据和程序绑定]]]==========", e);
		}
	}

	/**
	 * 字符串转化成为16进制字符串
	 * 
	 * @param s i
	 * @return
	 */
	public static int strTo16(String s, int i) {
		String str = s.replaceAll(" ", "");
		String str2 = Integer.valueOf(str, 16).toString();
		int j = Integer.parseInt(str2) / i;
		return j;
	}

	/**
	 * 字符串转化成为16进制字符串
	 * 
	 * @param s i
	 * @return
	 */
	public static long dateToStamp(String s) throws ParseException {
		String res;
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yy/MM/dd HH:mm:ss");
		Date date = simpleDateFormat.parse(s);
		long ts = date.getTime();
		return ts;
	}
}
